#include <iostream>
#include <string>
#include <fstream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>

#include "include/Configuration.h"
#include "include/Session.h"

using namespace std;
using namespace boost::property_tree;

void Configuration::Init(string & config_file_name)
{
	_config_file_name = config_file_name;
}

int Configuration::ReadConfig()
{
	ptree pt;
	read_json<ptree>(_config_file_name, pt);

	port = pt.get<int>("port");
	control_port = pt.get<int>("control_port");
	thread_count = pt.get<int>("thread_count");
	protocol = pt.get<int>("protocol");
	version = pt.get<int>("version");

	cout << "port: " << port << endl;
	cout << "control_port: " << control_port<< endl;
	cout << "thread_count: " << thread_count << endl;
	cout << "protocol: " << protocol << endl;
	cout << "version: " << version << endl;

	ptree pt1, pt2;
	pt1 = pt.get_child("server_info");

	for(ptree::iterator it = pt1.begin(); it != pt1.end(); it++)
	{
		pt2 = it->second;

		GameServerInfo *p_info = new GameServerInfo();
		p_info->id = pt2.get<int>("id");
		p_info->type = pt2.get<int>("type");
		p_info->ip = pt2.get<string>("ip");
		p_info->port = pt2.get<int>("port");

		int key = Logic::ServerKey(p_info->type, p_info->id);

		server_info_map[key] = p_info;

		cout << "--- server info begin ---" << endl;
		cout << "type and id: " << key << endl;
		cout << "ip: " << p_info->ip << endl;
		cout << "port" << p_info->port << endl;
		cout << "--- server info end   ---" << endl;
	}

	return 0;
}
